package com.smart.campus.school.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.smart.campus.common.utils.DateUtils;
import com.smart.campus.school.domain.ExamAnswer;
import com.smart.campus.school.domain.ExamPaper;
import com.smart.campus.school.domain.ExamQuestion;
import com.smart.campus.school.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.smart.campus.school.domain.StudentPaper;
import com.smart.campus.school.service.IStudentPaperService;
import com.smart.campus.common.core.text.Convert;

/**
 * 学生练习试卷Service业务层处理
 * 
 * @author Zhihe.Li
 * @date 2020-06-17
 */
@Service
public class StudentPaperServiceImpl implements IStudentPaperService 
{
    @Autowired
    private StudentPaperMapper studentPaperMapper;


    @Autowired
    private StudentPaperRelationMapper studentPaperRelationMapper;

    @Autowired
    private ExamAnswerMapper examAnswerMapper;

    @Autowired
    private ExamQuestionMapper examQuestionMapper;

    /**
     * 查询学生练习试卷
     * 
     * @param id 学生练习试卷ID
     * @return 学生练习试卷
     */
    @Override
    public StudentPaper selectStudentPaperById(Long id)
    {
        StudentPaper examPaper = studentPaperMapper.selectStudentPaperById(id);
        List<String> questionIdList = studentPaperRelationMapper.selectQuestionIdListByPaperId(id);
        String questionIds = String.join(",", questionIdList);
        examPaper.setQuestionIds(questionIds);
        List<ExamQuestion> questionLis = new ArrayList<>();
        for (String questionId : questionIdList) {
            ExamQuestion question = examQuestionMapper.selectExamQuestionById(Long.valueOf(questionId));
            questionLis.add(question);
            ExamAnswer examAnswer = new ExamAnswer();
            examAnswer.setQuestionId(question.getId());
            List<ExamAnswer> answerList = examAnswerMapper.selectExamAnswerList(examAnswer);
            question.setAnswerList(answerList);
        }
        examPaper.setQuestionList(questionLis);
        return examPaper;
    }

    /**
     * 查询学生练习试卷列表
     * 
     * @param studentPaper 学生练习试卷
     * @return 学生练习试卷
     */
    @Override
    public List<StudentPaper> selectStudentPaperList(StudentPaper studentPaper)
    {
        return studentPaperMapper.selectStudentPaperList(studentPaper);
    }

    /**
     * 新增学生练习试卷
     * 
     * @param studentPaper 学生练习试卷
     * @return 结果
     */
    @Override
    public int insertStudentPaper(StudentPaper studentPaper)
    {
        studentPaper.setCreateTime(DateUtils.getNowDate());
        return studentPaperMapper.insertStudentPaper(studentPaper);
    }

    /**
     * 修改学生练习试卷
     * 
     * @param studentPaper 学生练习试卷
     * @return 结果
     */
    @Override
    public int updateStudentPaper(StudentPaper studentPaper)
    {
        studentPaper.setUpdateTime(DateUtils.getNowDate());
        return studentPaperMapper.updateStudentPaper(studentPaper);
    }

    /**
     * 删除学生练习试卷对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteStudentPaperByIds(String ids)
    {
        return studentPaperMapper.deleteStudentPaperByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除学生练习试卷信息
     * 
     * @param id 学生练习试卷ID
     * @return 结果
     */
    @Override
    public int deleteStudentPaperById(Long id)
    {
        return studentPaperMapper.deleteStudentPaperById(id);
    }

    /**
     * 获取试卷答案
     *
     * @param id
     * @return
     */
    @Override
    public Map<Integer, Map<Long, List<ExamAnswer>>> getPaperAnswerByPaperId(Long id) {
        StudentPaper paper = studentPaperMapper.selectStudentPaperById(id);

        List<String> questionIdList = studentPaperRelationMapper.selectQuestionIdListByPaperId(id);
        Map<Long, List<ExamAnswer>> map = new LinkedHashMap<>();
        List<Long> idList = new ArrayList<>();
        for (String questionId : questionIdList) {

            idList.add(Long.valueOf(questionId));
        }
        List<ExamQuestion> questionList = examQuestionMapper.selectExamQuestionsByIds(idList);
        Map<Integer,Map<Long, List<ExamAnswer>>> typeMap = new LinkedHashMap<>();

        for (ExamQuestion question : questionList) {
            ExamAnswer examAnswer = new ExamAnswer();
            examAnswer.setQuestionId(Long.valueOf(question.getId()));
            List<ExamAnswer> answerList = examAnswerMapper.selectExamAnswerList(examAnswer);
            //按试题类型分组
            if(typeMap.containsKey(question.getType())){
                Map<Long, List<ExamAnswer>> resMap = typeMap.get(question.getType());
                resMap.put(Long.valueOf(question.getId()),answerList);
            }else {
                Map<Long, List<ExamAnswer>> resMap = new LinkedHashMap<>();
                resMap.put(Long.valueOf(question.getId()),answerList);
                typeMap.put(question.getType(),resMap);
            }

        }
        return typeMap;
    }
}
